/*
 * @Author: 唉诺
 * @Date: 2021-02-27 09:43:44
 */
import Phaser, {Easing} from 'phaser-ce'
import Enemy from '../base/enemy'

export default class extends Phaser.State {
  create () {
    this.bg = this.add.tileSprite(0, 0, this.game.width, this.game.height, 'bg')
    this.bg.tileScale.set(window.devicePixelRatio, window.devicePixelRatio)
    /* 开始框 */
    this.startGroup = this.add.group()
    this.bgAudio = this.add.audio('bgm_audio')
    this.bulletAudio = this.add.audio('bullet_audio')
    this.boomAudio = this.add.audio('boom_audio')
    this.hero = this.add.sprite(this.game.width / 2, 200, 'hero')

    this.hero.anchor.set(0.5, 0)
    this.hero.width = dpi(80)
    this.hero.height = dpi(80)

    this.physics.arcade.enable(this.hero)
    this.hero.body.collideWorldBounds = true
    this.physics.arcade.enable(this.hero)
    this.heroTween = this.add.tween(this.hero)
    this.heroTween.to({y: this.game.height - dpi(80)}, 1000, Phaser.Easing.Sinusoidal.InOut, true)
    this.heroTween.onComplete.add(this.startGame, this)

    // 击中动画
    this.heroHitTween = this.add.tween(this.hero)
    this.heroHitTween.to({alpha: 0.3}, 100, Easing.Linear.None, false, 0, 2, true)

    // 分数生命
    this.titleGroup = this.add.group()
    this.myLife = 10
    this.score = 0
    this.scoreText = this.add.text(10, 20, `得分:${this.score}`, {
      font: `${dpi(18)}px 微软雅黑`,
      fill: '#fff',
      align: 'center'
    }, this.titleGroup)
    this.scoreText.anchor.set(0, 0)

    this.lifeText = this.add.text(this.game.width - 10, 20, `生命:${this.myLife}`, {
      font: `${dpi(20)}px 微软雅黑`,
      fill: '#fff',
      align: 'center'
    }, this.titleGroup)
    this.lifeText.anchor.set(1, 0)
  }

  update () {
    if (this.myBulletGroup) {
      this.physics.arcade.overlap(this.myBulletGroup, this.enemys.enemysGroup, this.hitEnemy, null, this)
      this.physics.arcade.overlap(this.hero, this.enemys.enemyBulletGroup, (hero, bullet) => {
        this.hitHero(hero, bullet)
      })
      this.physics.arcade.overlap(this.hero, this.enemys.enemysGroup, (hero, enemy) => {
        this.boomHero(hero, enemy)
      })
    }
  };
  startGame () {
    this.hero.inputEnabled = true
    this.hero.input.enableDrag(false)
    this.bg.autoScroll(0, 50)
    this.bgAudio.play()
    // 我的子弹
    this.myBulletGroup = this.add.group()
    this.myBulletGroup.enableBody = true
    this.myBulletGroup.createMultiple(50, 'bullet')
    this.myBulletGroup.setAll('outOfBoundsKill', true)
    this.myBulletGroup.setAll('checkWorldBounds', true)
    this.myBulletGroup.setAll('width', dpi(16))
    this.myBulletGroup.setAll('height', dpi(30))
    this.enemys = new Enemy(this.game, {})

    this.isStart = true
    this.enemys.init()
    this.time.events.loop(1200, () => {
      if (this.isStart) {
        this.enemys.generateEnemy()
      }
    })
    this.time.events.loop(500, () => {
      this.openFire()
    })
  }

  hitHero (hero, bullet) {
    bullet.kill()
    if (this.myLife > 1) {
      this.myLife--
      this.lifeText.setText(`生命:${this.myLife}`)
      this.heroHitTween.start()
    } else {
      this.gameOver(hero)
      // 结束游戏
    }
  }
  boomHero (hero, enemy) {
    try {
      this.boomAudio.play()
    } catch (error) {

    }
    if (enemy && !enemy.isHit) {
      enemy.isHit = true
      enemy.loadTexture('boom', 0)
      enemy.animations.add('boom')
      enemy.play('boom', 110, false, true)
      this.gameOver()
    }
  }

  // 击中
  hitEnemy (hero, enemy) {
    if (!enemy.isHit) {
      enemy.isHit = true
      try {
        this.boomAudio.play()
      } catch (error) {

      }
      enemy.loadTexture('boom', 0)
      enemy.animations.add('boom')
      enemy.play('boom', 110, false, true)

      this.score++
      this.scoreText.setText(`得分:${this.score}`)
    }
  }
  // 我开火
  openFire () {
    if (this.isStart) {
      try {
        this.bulletAudio.play()
      } catch (err) {

      }
      const bullet = this.myBulletGroup.getFirstExists(false)
      bullet.reset(this.hero.x - 8, this.hero.y - 15)
      bullet.body.velocity.y = -dpi(400)
    }
  }

  // eslint-disable-next-line camelcase
  gameOver (show_text) {
    this.isStart = false
    this.myLife = 0
    this.lifeText.setText(`生命:${this.myLife}`)
    this.bg.autoScroll(0, 0)
    this.bgAudio.pause()

    this.hero.loadTexture('boom', 0)
    this.hero.animations.add('boom')
    this.hero.width = dpi(64)
    this.hero.height = dpi(48)
    this.hero.play('boom', 60, false, true)
    // 创建文字
    this.gameOverGroup = this.add.group()
    this.add.text(this.game.width / 2, 0, `游戏结束,点击重玩`, {
      font: `${dpi(20)}px 微软雅黑`,
      fill: '#fff',
      align: 'center'
    }, this.gameOverGroup).anchor.set(0.5, 0)
    this.add.button(this.game.width / 2, dpi(40), 'btn', () => {
      this.state.start('Play')
    }, null, null, null, null, null, this.gameOverGroup).anchor.set(0.5, 0)
    this.gameOverGroup.top = (this.game.height - this.gameOverGroup.height) / 2
  }
  render () {
    if (this.myBulletGroup) {
      // this.game.debug.text('Group size: ' + this.myBulletGroup.total, 32, 32)
      // this.game.debug.text('Group size: ' + this.enemys.enemysGroup.total, 32, 32)
    }
  }
}
